<?php
/* SVN FILE: $Id: pages_controller.php 7945 2008-12-19 02:16:01Z gwoo $ */
/**
 * Static content controller.
 *
 * This file will render views from views/pages/
 *
 * PHP versions 4 and 5
 *
 * CakePHP(tm) :  Rapid Development Framework (http://www.cakephp.org)
 * Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @filesource
 * @copyright     Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
 * @link          http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
 * @package       cake
 * @subpackage    cake.cake.libs.controller
 * @since         CakePHP(tm) v 0.2.9
 * @version       $Revision: 7945 $
 * @modifiedby    $LastChangedBy: gwoo $
 * @lastmodified  $Date: 2008-12-18 18:16:01 -0800 (Thu, 18 Dec 2008) $
 * @license       http://www.opensource.org/licenses/mit-license.php The MIT License
 */
/**
 * Static content controller
 *
 * Override this controller by placing a copy in controllers directory of an application
 *
 * @package       cake
 * @subpackage    cake.cake.libs.controller
 */
class PagesController extends AppController {
/**
 * Controller name
 *
 * @var string
 * @access public
 */
	var $name = 'Pages';
/**
 * Default helper
 *
 * @var array
 * @access public
 */
	var $helpers = array('Html');
	var $uses = array('City', 'User', 'Profile', 'Story', 'Visited');
/**
 * This controller does not use a model
 *
 * @var array
 * @access public
 */
	
/**
 * Displays a view
 *
 * @param mixed What page to display
 * @access public
 */
	function display() {
		$path = func_get_args();

		$count = count($path);
		if (!$count) {
			$this->redirect('/');
		}
		$page = $subpage = $title = null;

		if (!empty($path[0])) {
			$page = $path[0];
		}
		if (!empty($path[1])) {
			$subpage = $path[1];
		}
		if (!empty($path[$count - 1])) {
			$title = Inflector::humanize($path[$count - 1]);
		}
		$this->set(compact('page', 'subpage', 'title'));
		$this->render(join('/', $path));
	}
	
	function home()
	{
		$this->pageTitle = "Home";

		$this->Visited->recursive = -1;
		$visited = $this->Visited->find('all', array('limit' => 30, 'order' => 'RAND()'));
		foreach($visited as &$visit)
		{
			$this->City->recursive = -1;
			$city = $this->City->findById($visit['Visited']['city_id']);
			$this->Profile->recursive = -1;
			$profile = $this->Profile->findById($visit['Visited']['profile_id']);
			$this->User->recursive = -1;
			$visit_user = $this->User->findById($profile['Profile']['user_id']);
			
			foreach($city['City'] as $key => $val)
			 $visit['City']["$key"] = $val;
			 
			foreach($visit_user['User'] as $key => $val)
			 $visit['User']["$key"] = $val;
			 
		}
		//debug($visited);
		$this->set('visited',$visited);
		
		
		$this->City->recursive = 1;
		$this->Profile->recursive = 1;
		$this->Story->recursive = 1;
		$this->set('cities', $this->City->find('all', array('limit' => 15, 'order' => 'RAND()')));
		$this->set('profiles', $this->Profile->find('all', array('limit' => 50, 'order' => 'RAND()')));
		$this->set('stories', $this->Story->find('all', array('limit' => 50, 'order' => 'RAND()')));
		$this->User->recursive = 2;
		$this->User->unbindModel(array('hasAndBelongsToMany' => array('Friend')));
		$this->User->unbindModel(array('hasMany' => array('Story')));
		$this->User->Profile->unbindModel(array('hasMany' => array('Post')));
		$this->User->Profile->unbindModel(array('belongsTo' => array('City','User')));
		$users = $this->User->find('all', array('limit' => 10, 'order' => 'RAND()'));
		//debug($users);
		
		$this->set('users', $users);
		
		
	}
	
	function beforeFilter()
	{
		$this->Session->write('Auth.redirect', $this->here);
		$this->Auth->allow('home');
	}
}

?>